package com.example.administrator.menudialog319hz;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvInfo,tvMenu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        tvInfo=(TextView)findViewById(R.id.textView);
        tvMenu=(TextView)findViewById(R.id.textView2);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu1:
                tvInfo.setText("caidan1");
                showdialog();
                break;
            case R.id.menu2:
                tvInfo.setText("caidan2");
                break;
            case R.id.menu3:
                tvInfo.setText("caidan3");
                break;
            case R.id.menu4:
                tvInfo.setText("caidan4");
                break;
            case R.id.menu5:
                tvInfo.setText("caidan5");
                break;
            case R.id.menu6:
                tvInfo.setText("caidan6");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showdialog() {
        //动态加载布局
        final View view = getLayoutInflater().inflate(R.layout.dialoglayout,null);
        AlertDialog dialog = new AlertDialog.Builder(this)//1
        .setTitle("对话框标题")//2
        .setIcon(R.mipmap.ic_launcher)//3
        .setView(view)//4
                //.setMessage("111111111111111")
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText et = (EditText)view.findViewById(R.id.editText);
                String str = et.getText().toString();
                tvInfo.setText(str);
            }
        })//5
                .setNegativeButton("取消",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EditText et = (EditText)view.findViewById(R.id.editText);
                        String str = tvInfo.getText().toString();
                        tvInfo.setText(str);
                    }
                }).create();//6
        dialog.show();// 6 显示

    }
}
